﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YiSha.Business.YiDongYuan;
using YiSha.Entity.YiDongYuan;
using YiSha.Enum;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;

namespace YiSha.Admin.WebApi.Controllers
{
    /// <summary>
    /// 文件上传、下载、显示
    /// </summary>
    [Route("[controller]/[action]")]
    [ApiController]
    [AuthorizeFilter]
    public class FileController : ControllerBase
    {
        private BaseFileBLL baseFileBLL = new BaseFileBLL();

        #region 上传单个文件

        /// <summary>
        /// 上传单个文件
        /// </summary>
        /// <param name="fileModule"></param>
        /// <param name="fileList"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<TData<string>> UploadFile(int fileModule, IFormCollection fileList)
        {
            TData<string> obj = await FileHelper.UploadFile(fileModule, fileList.Files);
            if (fileModule == (int)UploadFileType.Mechanics || fileModule == (int)UploadFileType.DetectionVideo)
            {
                BaseFileEntity fileEntity = new BaseFileEntity()
                {
                    CreateTime = DateTime.Now,
                    FileModule = UploadFileType.Mechanics.ToString(),
                    FilePath = obj.Data,
                    Suffix = Path.GetExtension(obj.Data)
                };
                await baseFileBLL.SaveForm(fileEntity);
                obj.Data = fileEntity.Id.ToString();
            }
            return obj;
        }

        #endregion 上传单个文件

        #region 删除单个文件

        [HttpPost]
        public TData<string> DeleteFile(int fileModule, string filePath)
        {
            TData<string> obj = FileHelper.DeleteFile(fileModule, filePath);
            return obj;
        }

        #endregion 删除单个文件

        #region 下载文件

        [HttpGet]
        public FileContentResult DownloadFile(string filePath, int delete = 1)
        {
            TData<FileContentResult> obj = FileHelper.DownloadFile(filePath, delete);
            if (obj.Tag == 1)
            {
                return obj.Data;
            }
            else
            {
                throw new Exception("下载失败：" + obj.Message);
            }
        }

        #endregion 下载文件

        [HttpGet]
        public FileResult Show(long id)
        {
            var fileEntity = baseFileBLL.GetEntity(id).Result;
            if (fileEntity != null)
            {
                string filePath = fileEntity.Data.FilePath;
                string absoluteFilePath = GlobalContext.HostingEnvironment.ContentRootPath + Path.DirectorySeparatorChar + filePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
                byte[] fileBytes = System.IO.File.ReadAllBytes(absoluteFilePath);

                string fileNamePrefix = DateTime.Now.ToString("yyyyMMddHHmmss");
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
                string title = string.Empty;
                if (fileNameWithoutExtension.Contains("_"))
                {
                    title = fileNameWithoutExtension.Split('_')[1].Trim();
                }
                string fileExtensionName = Path.GetExtension(filePath);
                return File(fileBytes, "image/" + fileEntity.Data.Suffix.Replace(".", ""), fileNameWithoutExtension + fileExtensionName);
            }
            return null;
        }
    }
}